package com.ce.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ce.server.common.RespBean;
import com.ce.server.pojo.MenuRole;
import com.ce.server.mapper.MenuRoleMapper;
import com.ce.server.service.IMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-14
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

  @Autowired
  private MenuRoleMapper menuRoleMapper;
  @Autowired
  private RedisTemplate redisTemplate;

  /**
   * 更加角色菜单
   *
   * @param rid
   * @param mids
   * @return
   */
  @Override
  public RespBean updateMenuRole(Integer rid, Integer[] mids) {
    menuRoleMapper.delete(new LambdaQueryWrapper<MenuRole>().eq(MenuRole::getRid, rid));
    if (mids == null || mids.length == 0) {
      return RespBean.success("更新成功！");
    }
    int recordNum = menuRoleMapper.insertRecords(rid, mids);
    if (recordNum == mids.length) {
      redisTemplate.delete(redisTemplate.keys("*"));
      return RespBean.success("角色菜单更新成功！");
    }

    return RespBean.error("角色菜单更新失败！");
  }
}
